package cn.com.dhc.danlu.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.com.dhc.danlu.R;

/**
 * @author wuhaiyang
 */
public class DLToolbar extends RelativeLayout {
    private TextView tvTitle;
    private TextView tvRight;
    private ImageView ivRight;
    private ImageView ivBack;

    private String title;
    private String right;
    private Drawable leftImgRes;
    private Drawable rightImgRes;
    private boolean leftVisible;
    private int titleColor;

    public DLToolbar(Context context) {
        this(context, null);
    }

    public DLToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DLToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initAttrs(context, attrs);
        initViews();
        bindAction();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DLToolbar);
        title = ta.getString(R.styleable.DLToolbar_toolbarTitle);
        right = ta.getString(R.styleable.DLToolbar_toolbarRight);
        leftImgRes = ta.getDrawable(R.styleable.DLToolbar_toolbarLeftImg);
        rightImgRes = ta.getDrawable(R.styleable.DLToolbar_toolbarRightImg);
        leftVisible = ta.getBoolean(R.styleable.DLToolbar_toolbarLeftVisible, true);
        titleColor = ta.getColor(R.styleable.DLToolbar_toolBarTitleColor, getResources().getColor(R.color.color_444444));
        ta.recycle();
    }

    private void initViews() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_toolbar, this, true);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvRight = (TextView) findViewById(R.id.tv_right);
        ivRight = (ImageView) findViewById(R.id.iv_right);

        setBackVisible(leftVisible);

        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
            tvTitle.setTextColor(titleColor);
        }

        if (!TextUtils.isEmpty(right)) {
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(right);
            // 如果文字可见, 图片将不可见
            ivRight.setVisibility(GONE);
            tvRight.setTextColor(titleColor);
            rightImgRes = null;
        } else {
            tvRight.setVisibility(GONE);
        }

        if (null != rightImgRes) {
            ivRight.setVisibility(VISIBLE);
            ivRight.setImageDrawable(rightImgRes);
        } else {
            ivRight.setVisibility(GONE);
        }
    }

    private void bindAction() {
        ivBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        });
    }

    public void setRightImg(@DrawableRes int resId) {
        ivRight.setVisibility(VISIBLE);
        tvRight.setVisibility(GONE);
        ivRight.setImageResource(resId);
    }

    public void setBackImg(@DrawableRes int resId){
        ivBack.setImageResource(resId);
    }

    public void setRightTxt(@StringRes int resId) {
        tvRight.setVisibility(VISIBLE);
        ivRight.setVisibility(GONE);
        tvRight.setText(resId);
    }

    public void setRightTxt(String text) {
        tvRight.setVisibility(VISIBLE);
        ivRight.setVisibility(GONE);
        tvRight.setText(text);
    }

    public void setBackVisible(boolean isVisible) {
        ivBack.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        if (isVisible) {
            if (null != leftImgRes) {
                ivBack.setImageDrawable(leftImgRes);
            } else {
                ivBack.setImageResource(R.drawable.ic_back_img);
            }
        }
    }

    public void setBackListener(OnClickListener listener) {
        if (null != listener) {
            ivBack.setOnClickListener(listener);
        }
    }

    public void setRightClickListenter(OnClickListener listener) {
        if (null != listener) {
            if (tvRight.getVisibility() == VISIBLE) {
                tvRight.setOnClickListener(listener);
                return;
            }
            if (ivRight.getVisibility() == VISIBLE) {
                ivRight.setOnClickListener(listener);
            }
        }
    }

    public void setTitle(@StringRes int resId) {
        tvTitle.setText(resId);
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }
}